package com.huizu.api.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.huizu.api.service.IMemberApiService;
import com.huizu.api.utils.PageUtil;
import com.huizu.common.core.domain.AjaxResult;
import com.huizu.common.utils.OssUtil;
import com.huizu.common.utils.StringUtils;

@Controller
@RequestMapping("/douMeiHui/member/api")
public class MemberApiController {

	@Autowired
	private IMemberApiService service;

	// 会员注册
	@PostMapping("/registration")
	@ResponseBody
	public Map<String, Object> getShopList(@RequestBody Map<String, Object> param) {
		Map<String, Object> rem = new HashMap<String, Object>();
		try {
			if (service.insertMember(param) > 0) {
				rem.put("code", 0);
				rem.put("msg", "注册成功");
			} else {
				rem.put("code", 0);
				rem.put("msg", "注册失败");
			}
		} catch (Exception e) {
			Throwable cause = e.getCause();
			if (cause instanceof java.sql.SQLIntegrityConstraintViolationException) {
				String errMsg = ((java.sql.SQLIntegrityConstraintViolationException) cause).getMessage();
				if (StringUtils.isNotBlank(errMsg) && errMsg.indexOf("phone") != -1) {
					rem.put("code", "0");
					rem.put("msg", "已注册");
				} else {
					rem.put("code", "1");
					rem.put("msg", "系统错误,请稍后重试");
				}
			} else {
				rem.put("code", "1");
				rem.put("msg", "注册失败,请稍后重试");
			}
			e.printStackTrace();
		}
		return rem;
	}

	// 编辑用户信息
	@PostMapping("/updateMember")
	@ResponseBody
	public Map<String, Object> updateMember(@RequestBody Map<String, Object> param) {
		Map<String, Object> rem = new HashMap<String, Object>();
		try {
			service.updateMember(param);
			rem.put("code", 0);
			rem.put("msg", "编辑成功");
		} catch (Exception e) {
			rem.put("code", 1);
			rem.put("msg", "编辑失败");
			e.printStackTrace();
		}
		return rem;
	}
	
	// 查询用户信息
	@PostMapping("/getMember")
	@ResponseBody
	public Map<String, Object> getMember(@RequestBody Map<String, Object> param) {
		Map<String, Object> rem = new HashMap<String, Object>();
		try {
			rem.put("data", service.getMemberNew(param));
			rem.put("code", 0);
			rem.put("msg", "查询成功");
		} catch (Exception e) {
			rem.put("code", 1);
			rem.put("msg", "查询失败");
			e.printStackTrace();
		}
		return rem;
	}
	
	/**
	 * 通用上传请求
	 */
	@PostMapping("/uploadFile")
	@ResponseBody
	public Map<String, Object> uploadFile(MultipartFile file, HttpServletRequest request) throws Exception {
		Map<String, Object> rem = new HashMap<String, Object>();
		try {
			// 上传文件路径
			String url = OssUtil.uploadIimage(file);
			OssUtil.removeImage(file, request);
			rem.put("data", url);
			rem.put("code", 0);
			rem.put("msg", "查询成功");
		} catch (Exception e) {
			rem.put("code", 1);
			rem.put("msg", "查询失败");
			e.printStackTrace();
		}
		return rem;
	}

}
